package com.zy.fee.roadpark.activity;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.SystemClock;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.jpush.android.api.JPushInterface;
import com.flyco.roundview.RoundTextView;
import com.zy.fee.roadpark.R;
import com.zy.fee.roadpark.config.Constants;
import com.zy.fee.roadpark.view.CommonDialog;
import com.zygk.library.base.BaseActivity;

/**
 * Created by Wikison on 2018/9/20.
 */
public class SettingActivity extends BaseActivity {
  @BindView(R.id.lh_tv_title) TextView lhTvTitle;
  @BindView(R.id.tv_version) TextView tvVersion;
  @BindView(R.id.tv_button) RoundTextView tvButton;
  private static final int EXIT = 0x001;
  @BindView(R.id.tv_exit) TextView tvExit;

  @Override protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ButterKnife.bind(this);
  }

  @Override public void setContentView() {
    setContentView(R.layout.activity_setting);
  }

  @Override public void init() {
    lhTvTitle.setText("设置");

    tvVersion.setText("V " + getVersionName());

    initListener();
  }

  private void initListener() {
    tvExit.setOnClickListener(new View.OnClickListener() {
      final static int COUNTS = 5;//点击次数
      final static long DURATION = 3 * 1000;//规定有效时间
      long[] mHits = new long[COUNTS];

      @Override public void onClick(View v) {
        System.arraycopy(mHits, 1, mHits, 0, mHits.length - 1);
        //实现左移，然后最后一个位置更新距离开机的时间，如果最后一个时间和最开始时间小于DURATION，即连续5次点击
        mHits[mHits.length - 1] = SystemClock.uptimeMillis();
        if (mHits[0] >= (SystemClock.uptimeMillis() - DURATION)) {
          sendBroadcast(new Intent(Constants.BROADCAST_EXIT));
        }
      }
    });
  }

  @Override protected void handleReceiver(Context context, Intent intent) {
    super.handleReceiver(context, intent);
    if (intent == null || TextUtils.isEmpty(intent.getAction())) {
      return;
    }
  }

  @OnClick({
      R.id.ll_back, R.id.tv_button, R.id.ll_modify_password, R.id.ll_app
  }) public void onViewClicked(View view) {
    Intent intent;
    switch (view.getId()) {
      case R.id.ll_back:
        finish();
        break;
      case R.id.ll_modify_password:
        intent = new Intent(mContext, ModifyPasswordActivity.class);
        startActivity(intent);
        break;
      case R.id.ll_app:
        intent = new Intent();
        intent.setAction("android.intent.action.VIEW");
        Uri content_url = Uri.parse(Constants.APK_URL);
        intent.setData(content_url);
        startActivity(intent);
        break;
      case R.id.tv_button:
        CommonDialog.showYesOrNoWithTitleDialog(mContext, "", "确定要退出吗? ", "取消", "确定", () -> {
          preferencesHelper.clearData();
          JPushInterface.deleteAlias(mContext, 0);
          Intent i = new Intent(mContext, LoginActivity.class);
          startActivity(i);
        }, () -> {
        });
        break;
    }
  }
}
